// pages/zhuti/zhuti.js
// 引入common公共方法
let http = require('../../utils/api.js');
let domain = require('../../utils/api.js');
import {
    toData,
    myHttps,
    login
} from '../../utils/util.js';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        hiddenLoading: false, //显示正在加载
        shows: false, // 点击去认领
        themeIds: '', //首页跳转带过来的主题id
        // 关注
        focus: false,
        List: '',
        images: '', //判断是否有图片
        articleId: '',
        articles: [], //拿到的文章列表数据
        collectid: '', //点三个点获取的id
        isFocus: '',
        followList: '', //订阅主题的用户列表数据
        total: '', //订阅主题的用户数量
        fav: '', //收藏接口是否收藏
        detailContent: '', //分享页面需要得内容
        detailImg: '',
        detailTitle: '',
        page:1,//当前页
        size:10,//页容量
        userId:'',//用户id
        toId:'',//创建此主题得用户id
        domain:'',
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(e) {
        this.setData({
            themeIds: e.id,
            userId: wx.getStorageSync('userId'),
            domain:domain.domain
        })
        this.themeHomes(), //调用主题详情接口
        this.FollowThemeList(), //调用订阅主题的关注列表接口
        this.essaysList() //调用文章列表接口
    },
    //下拉刷新从新初始化页码,条数
    onPullDownRefresh: function() {
        this.setData({
            page:1,
            size:10,
            articles: [],
        })
        //下拉三个点消失
        wx.stopPullDownRefresh();
        this.themeHomes(), //调用主题详情接口
        this.FollowThemeList(), //调用订阅主题的关注列表接口
        this.essaysList() //调用文章列表接口
    },
    // 上拉加载数据
    onReachBottom: function () {
        this.setData({
            size: 10,
            page: this.data.page + 1,
        })
        this.essaysList() //调用文章列表接口
    },
    // 点击作者订阅专题 跳转到订阅列表
    goamountFollow(e) {
        var id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '../amountFollow/amountFollow?id=' + id,
            success: function(res) {},
            fail: function(res) {},
            complete: function(res) {},
        })
    },
    // 点击每条 去文章详情
    goDetails(e) {
        let id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '../details/details?id=' + id
        });
    },
    // 关闭认领主题
    close() {
        this.setData({
            shows: false
        });
    },
    // +关注
    addfocus(e) {
        var id = e.currentTarget.dataset.id;
        let url = http.api.followtheme + id + '/fav';
        myHttps({
            url: url,
            method: "POST",
        }).then(res => {
            if (res.code == 1) {
                this.data.List.fav_count = this.data.List.fav_count+1
                this.setData({
                    isFocus: true,
                    List: this.data.List
                })
                this.FollowThemeList(); //调用订阅主题的关注列表接口
            }
        }).catch(err => {})
    },
    // 点击已经关注 取消关注
    cancelfocus(e) {
        var id = e.currentTarget.dataset.id;
        // console.log(id)
        let url = http.api.delFollow + id + '/fav';
        myHttps({
            url: url,
            method: "DELETE",
        }).then(res => {
            if (res.code == 1) {
                this.data.List.fav_count = this.data.List.fav_count -1
                this.setData({
                    isFocus: false,
                    List: this.data.List
                })
                this.FollowThemeList(); //调用订阅主题的关注列表接口
            }
        }).catch(err => {})
    },
    // go编辑主题
    goEditorTheme() {
        // console.log(2)
        wx.setStorageSync('themeid', this.data.themeId)
        //清除之前得主题缓存数据
        wx.removeStorage({
            key: 'apply',
        })
        wx.removeStorage({
            key: 'title',
        })
        wx.removeStorage({
            key: 'wgList',
        })
        wx.removeStorage({
            key: 'mediaData',
        })
        wx.removeStorage({
            key: 'avatar',
        })
        wx.navigateTo({
            url: '../EditorTheme/EditorTheme',
        })
    },
    // 点击留言
    goMessage(e) {
        var id = e.currentTarget.dataset.id
        var isAcatar;
        if (this.data.toId == this.data.userId){//是作者
            isAcatar = true
        }else {//不是作者
            isAcatar = false
        }
        // console.log(isAcatar)
        wx.navigateTo({
            url: '../message/message?id=' + id + '&isAcatar=' + isAcatar
        });
    },
    // 主题详情接口 获取主题主页
    themeHomes() {
        let id = this.data.themeIds
        let url = http.api.themeHome + id;
        myHttps({
            url: url,
            method: "GET",
        }).then(res => {
            if (res.code == 1) {
                // console.log(res.data)
                this.setData({
                    List: res.data,
                    isFocus: res.data.isFav,
                    // articles: res.data.articles,
                    // images: res.data.articles.images
                    hiddenLoading: true,
                    toId: res.data.user.id,//创建此主题得用户id
                    themeId: res.data.id
                })
                console.log(this.data.List)
                // console.log(this.data.toId)
                // console.log(this.data.userId)
            }
        }).catch(err => {})
    },
    // 获取主题下文章列表接口
    essaysList() {
        let id = this.data.themeIds
        let url = http.api.themeessays + id + '/essays?page=' + this.data.page + '&size='+this.data.size;
        myHttps({
            url: url,
            method: "GET",
        }).then(res => {
            if (res.code == 1) {
                for (var i = 0; i < res.data.content.length; i++) {
                    res.data.content[i].essay.pubdate = toData(res.data.content[i].essay.pubdate,1)
                    this.data.articles.push(res.data.content[i])
                }
                this.setData({
                    articles: this.data.articles,
                    hiddenLoading: true
                })
            }
        }).catch(err => {})
    },
    // 关注主题的用户列表
    FollowThemeList() {
        let id = this.data.themeIds
        let url = http.api.FollowThemeList + id + '/fav/users' + '?page=' + 1 + '&size=' + 1;
        myHttps({
            url: url,
            method: "GET",
        }).then(res => {
            if (res.code == 1) {
                this.setData({
                    followList: res.data.content,
                    total: res.data._meta.total
                })
            }
        }).catch(err => {})
    },
    // 分享弹窗收藏
    collects(e) {
        var id = this.data.collectid
        var fav = this.data.fav
        // console.log(id)
        // console.log(fav)
        if (fav == true) {
            this.cancelcollect(id) //取消收藏
        } else if (fav == false) {
            this.collect(id) //点击收藏
        }
        this.setData({
            show: false
        });
    },
    // 点击收藏 接口
    collect(id) {
        // let id = this.data.data.id
        let url = http.api.collects + id + '/fav';
        myHttps({
            url: url,
            method: "POST",
        }).then(res => {
            if (res.code == 1) {
                this.setData({
                    // scrollTop: 0,
                    collect: true,
                    // isFav: true
                })
                wx.showToast({
                    title: '收藏成功',
                    icon: 'success',
                    duration: 1000
                })
            }
        }).catch(err => {})
    },
    // 取消收藏
    cancelcollect(id) {
        // let id = this.data.data.id
        let url = http.api.collects + id + '/fav';
        myHttps({
            url: url,
            method: "DELETE",
        }).then(res => {
            if (res.code == 1) {
                this.setData({
                    // scrollTop: 0,
                    collect: false,
                    // isFav: false
                })
                wx.showToast({
                    title: '取消收藏',
                    icon: 'success',
                    duration: 1000
                })
            }
        }).catch(err => {})
    },
    // 弹窗
    showss(e) {
        // console.log(e)
        var id = e.currentTarget.dataset.id
        var fav = e.currentTarget.dataset.fav
        this.setData({
            collectid: id,
            fav: fav,
            detailContent: e.currentTarget.dataset.content,
            detailImg: e.currentTarget.dataset.img,
            detailTitle: e.currentTarget.dataset.title,
        })
        this.setData({
            show: true
        });
    },
    // 弹窗取消的点击事件
    cancel() {
        this.setData({
            show: false
        });
    },
    // 分享弹窗 转发朋友圈
    Share() {
        wx.showLoading({
            title: '加载中',
        })
        setTimeout(function() {
            wx.hideLoading()
        }, 2000)
        wx.navigateTo({
            url: '../picture/picture?title=' + this.data.detailTitle + '&content=' + this.data.detailContent + '&id=' + this.data.collectid,
        })
    },
    // 分享 页面
    onShareAppMessage: function(res) {
        if (res.from === 'button') {
            // 来自页面内转发按钮
            // console.log(res)
            this.cancel()
        }
        // console.log(res)
        return {
            title: this.data.detailTitle,
            imageUrl: this.data.detailImg,
            path: '/pages/details/details?id=' + this.data.collectid
        }
        this.setData({
            show: false
        });
    },
})